<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2021/12/22
 * Time: 1:25 下午
 */

namespace app\common\cachemodel\live;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

/**
 * Class LiveSubscribeLive
 * @package app\common\cachemodel\live
 */
class LiveSubscribe extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_subscribe';
    protected $cacheModel = 'live.subscribe';

    /**
     * 获取用户预约信息
     * @param $uid
     * @param $borId
     * @return array|mixed
     * @throws \Exception
     */
    public function getUserSubscribeId($uid, $borId)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid . '-' . $borId)->getDataAndSet(function () use ($uid, $borId)
        {
            try
            {
                $data = $this->where(['uid' => $uid, 'broadcast_id' => $borId])->field('id,status')->findOrFail()->toArray();
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        });
        if (is_string($data))
        {
            $data = json_decode($data, true);
        }
        return $data;
    }

    /**
     * @param $data
     * @return bool
     * @throws \Exception
     */
    public function saveData($data)
    {
        try
        {
            $this->insertGetId($data);
        }
        catch (\Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * @param $where
     * @param $data
     * @return bool
     * @throws \Exception
     */
    public function updateData($where, $data)
    {
        try
        {
            $this->where(['id' => $where['id']])->update($data);
        }
        catch (\Exception $e)
        {
            return false;
        }
        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $where['uid'] . '-' . $where['borId'])->rmCache();
        return true;
    }
}
